﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Spring.Context.Support;
using Huirui.Cavan.Core.Extension;
namespace Huirui.Cavan.Application.MainBoundedContext.SystemModule
{
    public class EmployeeAppService : BaseAppService<Employee, string, EmployeeSearch, IEmployeeRepository>, IEmployeeAppService
    {
        private IJobAppService JobAppService { get; set; }

        public override BoolGenericResult<Employee> AddOrUpdate(Employee entity)
        {
            this.SpringContext = ContextRegistry.GetContext();
            BoolGenericResult<Employee> result = new BoolGenericResult<Employee>();

            this.JobAppService = this.SpringContext.GetObject(SystemModuleConst.JOBAPPSERVICE) as IJobAppService;

            Employee employee = this.GetBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id)).FirstOrDefault();
            Administrator admin = new Administrator(employee);

            if (entity.Job.IsNotNull() && entity.Job.Id > 0)
            {
                entity.Job = this.JobAppService.GetById(entity.Job.Id);
            }
            Employee oldEmployee = null;

            if (entity.Id.IsNotEmpty())
            { 
                oldEmployee = this.Repository.Get(entity.Id);
            }
            if (oldEmployee.IsNull())
            {
                IIDGenerateAppService idGenerateAppService = this.SpringContext.GetObject(SystemModuleConst.IDGENERATEAPPSERVICE, new object[] { employee.Corporation }) as IIDGenerateAppService;
                if (entity.Id.IsEmpty())
                {
                    entity.Id = idGenerateAppService.GenerateID(SystemIdentifier.BaseCodeRuleType.Employee);
                }
                else
                {
                    idGenerateAppService.GenerateID(SystemIdentifier.BaseCodeRuleType.Employee);
                }

                result = admin.AddEmployee(entity.Id, entity.Name, entity.Job, entity.CommissionRate, entity.Sex, entity.Birthday, entity.Email, entity.Mobile, entity.SMSQueryEnabled, entity.Remark, entity.ManagerEmployeeCode, entity.EditUser, entity.Department, entity.UserAccount);
                if (result.IsSuccess)
                    return base.Add(result.Object);
            }
            else
            {
                result = admin.UpdateEmployee(oldEmployee, entity.Name, entity.Job, entity.CommissionRate, entity.Sex, entity.Birthday, entity.Email, entity.Mobile, entity.SMSQueryEnabled, entity.Remark, entity.EditUser, entity.Department);
                if (result.IsSuccess)
                    return base.Update(result.Object);
            }
            return result;
        }


        public override BoolGenericResult<Employee> Delete(Employee entity)
        {
            if (SecurityAppService.UserAccount.Role.Id == 1)
            {
                BoolGenericResult<Employee> result = new BoolGenericResult<Employee>();
                result.IsSuccess = false;
                result.Messages = new List<string> { "You can't delete this admin user" };
                return result;
            }
            entity.IsDeleted = true;
            return base.Update(entity);
        }
    }
}
